package com.type;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import ShareLife.gar.ToolOld;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Parcel;
import android.os.Parcelable;

public class Photo implements Parcelable {
	private int photoWidth;
	private int photoHeight;
	private byte[] photoByteArray;

	
	public Photo() {

	}
	

	public Photo(int photoWidth, int photoHeight, byte[] photoByteArray) {
		this.photoWidth = photoWidth;
		this.photoHeight = photoHeight;
		this.photoByteArray = photoByteArray;
	}
	
	
	public Photo(Bitmap bitmap) {
		photoWidth = bitmap.getWidth();
		photoHeight = bitmap.getHeight();
		int intArray[] = new int[photoWidth * photoHeight];
		bitmap.getPixels(intArray, 0, photoWidth, 0, 0, photoWidth, photoHeight);
		photoByteArray = ToolOld.int2byte(intArray);
	}

	
	public Bitmap fetchBitmap() {
		return Bitmap.createBitmap(ToolOld.byte2int(photoByteArray), photoWidth, photoHeight, Config.ARGB_8888);
	}

	
	public void receive(DataInputStream dis) throws IOException, InterruptedException {
		photoWidth = dis.readInt();
		photoHeight = dis.readInt();
		photoByteArray = new byte[((photoWidth * photoHeight) << 2)];

		int currentIndex = 0;
		int storeByteNumber = 0;
		while (currentIndex != photoByteArray.length) {
			storeByteNumber = photoByteArray.length - currentIndex >= 512 ? 512 : photoByteArray.length - currentIndex;
			byte tempArray[] = new byte[storeByteNumber];
			dis.read(tempArray, 0, storeByteNumber);
			System.arraycopy(tempArray, 0, photoByteArray, currentIndex, storeByteNumber);
			currentIndex += storeByteNumber;
		}

		Thread.sleep(150);
	}

	
	public void send(DataOutputStream dos) throws IOException, InterruptedException {
		dos.writeInt(photoWidth);
		dos.writeInt(photoHeight);

		int currentIndex = 0;
		int storeByteNumber = 0;
		while (currentIndex != photoByteArray.length) {
			storeByteNumber = photoByteArray.length - currentIndex >= 512 ? 512 : photoByteArray.length - currentIndex;
			byte tempArray[] = new byte[storeByteNumber];
			System.arraycopy(photoByteArray, currentIndex, tempArray, 0, storeByteNumber);
			currentIndex += storeByteNumber;
			dos.write(tempArray, 0, storeByteNumber);
		}

		Thread.sleep(150);
	}

	

	public Photo(Parcel in) {
		photoWidth = in.readInt();
		photoHeight = in.readInt();
		
	    int length = in.readInt();
	    if (length > 0) {
	        photoByteArray = new byte[length];
	        in.readByteArray(photoByteArray);
	    }
	}
	
	
	public static final Parcelable.Creator<Photo> CREATOR = new Parcelable.Creator<Photo>() {
		public Photo createFromParcel(Parcel in) {
			return new Photo(in);
		}

		@Override
		public Photo[] newArray(int size) {
			return new Photo[size];
		}
	};

	
	@Override
	public int describeContents() {
		return 0;
	}

	
	@Override
	public void writeToParcel(Parcel out, int flags) {
		out.writeInt(photoWidth);
		out.writeInt(photoHeight);
		
		if (photoByteArray == null)
            out.writeInt(0);
		else {
			out.writeInt(photoByteArray.length);
			out.writeByteArray(photoByteArray);
		}
	}
}
